#!/usr/bin/env ruby

# Copyright (c) 2010-2012 Microsoft Corp.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#	http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

require 'rubygems'
require 'ruby-aws'
require 'optparse'
require 'digest/md5'
require 'rexml/document'

require './include/config'
require './include/proxy'
require './include/requester'
require './include/templates'

@config = MOS::Config.create
@mturk = Amazon::WebServices::MechanicalTurkRequester.new :Host => @config[:Server]

def parseAnswer(assignment)
	answer = MOS.parseAnswer(assignment[:Answer])
	answer[:AssignmentId] = assignment[:AssignmentId]
	answer[:WorkerId]     = assignment[:WorkerId]
	answer[:SubmitTime]   = assignment[:SubmitTime]
	answer[:WorkingTime]  = assignment[:SubmitTime] - assignment[:AcceptTime]
	answer[:Status]       = assignment[:AssignmentStatus]
	answer
end

def getAllAssignments(hits)
	assignment_list = Array.new(hits.length)

	tp = Amazon::Util::ThreadPool.new(@config[:ThreadCount])
	hits.each_index do |i|
		tp.addWork(i) do |i|
			hit_id = hits[i][:HITId]
			begin
				hit = @mturk.GetHIT( :HITId => hit_id )[:HIT]
				printf("Retrieving assignments from HIT with Id %s and Status %s\n", hit[:HITId], hit[:HITStatus])
				assignment_list[i] = @mturk.collect_assignments_from_HITId(hit_id)
			rescue => e
				printf("Error fetching HIT with ID %s: %s\n", hit_id, e.message)
			end
		end
	end
	tp.finish

	assignment_list.each do |a_list|
		if a_list != nil
			@results += a_list.map { |a| parseAnswer(a) }
		end
	end
end

@options = { }
optparse = OptionParser.new do|opts|
	opts.banner = "Usage: #{$0} [options]"
 
	@options[:quiet] = false
	opts.on( '-q', '--quiet', 'Do not print any messages (implies -f)' ) do
		@options[:quiet] = true
	end

	@options[:all] = false
	opts.on( '-a', '--all', 'Retrieve all assignments in account' ) do
		@options[:all] = true
	end

	@options[:force] = false
	opts.on( '-f', '--force', 'Do not ask for confirmation' ) do
		@options[:force] = true
	end
 
	opts.on( '-h', '--help', 'Display this screen' ) do
		puts opts
		exit
	end

	opts.on_tail("")
	opts.on_tail("This script collects all submitted assignments from HITs in the Reviewable state.")
	opts.on_tail("For a HIT to become reviewable, every one of its available assignments must have been submitted.")
end

begin
	optparse.parse!
rescue OptionParser::ParseError => e
	$stderr.puts("Error parsing command-line arguments: " + e.message)
	$stderr.puts("Try '#{$0} --help' for more information.")
	exit
end

if @options[:quiet]
	@options[:force] = true
	devnull = File.exist?('/dev/null') ? '/dev/null' : 'NUL'
	$stdout = File.new(devnull, 'w')
end

if @options[:all] && !@options[:force]
	printf("Are you sure you want to download all assignments from all reviewable HITs? (y/n) ")
	response = STDIN.gets.chomp.downcase
	if response != 'y'
		printf("Exiting...\n")
		exit
	end
end

@results = []

if @options[:all]
	hits = @mturk.collect_reviewable_HITs()
	getAllAssignments(hits)
else
	if !File.exist?(@config[:HITSuccessFile])
		$stderr.puts("No HITs have been submitted yet. Please run CreateHITs.rb first.")
		exit
	end

	hits = Amazon::Util::DataReader.load(@config[:HITSuccessFile], :Tabular)
	getAllAssignments(hits)
end

if File.exist?(@config[:HITResultsFile])
	FileUtils.move(@config[:HITResultsFile], @config[:HITResultsFile] + '.backup')
end
Amazon::Util::DataReader.save(@config[:HITResultsFile], @results, :Tabular)

